<template>
  <div class="vux-form-preview weui-form-preview">
    <div class="weui-form-preview__bd">
      <div
        v-for="item in list"
        :key="item.key"
        class="weui-form-preview__item"
        @click.prevent="card_onclick(list)"
      >
        <template>
          <template v-if="['char', 'text', 'integer', 'float', 'date', 'datetime'].indexOf(item.type) >= 0">
            <label class="weui-form-preview__label">{{ item.title }}:</label>
            <span class="weui-form-preview__value">{{ item.value }}</span>
          </template>
          <template v-else-if="item.type === 'boolean'">
            <label class="weui-form-preview__label">{{ item.title }}</label>
            <template v-if="item.value">
              <span class="weui-form-preview__value"><icon type="success" /></span>
            </template>
            <template v-else>
              <span class="weui-form-preview__value"><icon type="cancel" /></span>
            </template>
          </template>
          <template v-if="['many2one', 'selecttion'].indexOf(item.type) >= 0">
            <label class="weui-form-preview__label">{{ item.title }}</label>
            <span class="weui-form-preview__value">{{ item.options?item.options[0].value:'' }}</span>
          </template>
          <template v-if="['selecttion'].indexOf(item.type) >= 0">
            <label class="weui-form-preview__label">{{ item.title }}</label>
            <span class="weui-form-preview__value">{{ item.options?item.options[0].value:'' }}</span>
          </template>
          <template v-else-if="!item.invisible && item.type === 'button'">
            <div :have_button="button=true" />
          </template>
        </template>
      </div>
    </div>
    <div
      v-show="button"
      class="weui-form-preview__ft"
    >
      <template v-for="item in list">
        <a
          v-show="!item.invisible && item.type === 'button'"
          :key="item.title"
          class="weui-form-preview__btn"
          href="javascript:"
          :class="{'weui-form-preview__btn_default': item.style==='default', 'weui-form-preview__btn_primary': item.style === 'primary'}"
          :value="list.name.value"
          @click.prevent="buttonHttp(item, index)"
        >{{ item.title }}
        </a>
      </template>
    </div>
  </div>
</template>

<script>
  export default {
  name: 'CellFormPreview',
  components: {
  },
  props: {
    card: { type: Object, default () { return {} } },
    model: { type: String, default: '' },
    viewId: { type: Number, default: 0 },
    borderIntent: {
      type: Boolean,
      default: true
    }
  },
  data: function () {
    return {
      id: this.card.id,
      list: this.card,
      button: false
    }
  },

  methods: {
    card_onclick: function (list) {
      // this.$emit('on-click-card', list)
      this.$router.push({ 'name': 'Form', params: { model: this.model, active_id: list.id.value } })
    },
    buttonHttp: function (item, index) {
      let self = this
      if (item.value === 'change') {
        self.$router.push({
          name: 'NewForm',
          params: {
            id: self.card.id,
            model: self.model,
            viewId: self.viewId
          }
        })
      } else {
        self.$http.get('/odoo/mobile/button/method', {
          params: {
            method: item.value,
            model: item.model,
            ids: self.id
          }
        }).then(function (response) {
          if (response.success) {
            self.$emit('refresh', false)
          } else {
            // self.$emit('show-toast', { toastType: 'warn', toastMsg: response.errMsg, toastShow: true })
          }
        }).catch(function () {

        })
      }
    }
  }
}
</script>

<style >

</style>
